package com.hei.controller;

import com.hei.pojo.Barticle;
import com.hei.pojo.Bcomment;
import com.hei.service.BarticleService;
import com.hei.service.BcommentService;
import com.hei.service.BlabelService;
import com.hei.util.MardownUtil;
import com.hei.util.ResponseMessage;
import com.hei.util.uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class ArticleController {
    @Autowired
    private BarticleService barticleService;
    @Autowired
    private BcommentService bcommentService;

    @Autowired
    private BlabelService blabelService;

    @RequestMapping("/article/{id}")
    public String toArticle(@PathVariable("id")String id, Model model){
        Barticle barticle = barticleService.selectArticleById(id);
        barticle.setContent(MardownUtil.parse(barticle.getContent()));
        barticle.setLabel(blabelService.queryById(barticle.getLid()).getLabel());//将kid转换实体
        List<Bcomment> bcomments = bcommentService.selectById(id);
        model.addAttribute("comCount",bcommentService.selectCountById(id));
        model.addAttribute("article",barticle);
        model.addAttribute("comments",bcomments);

        return "article";
    }

    @RequestMapping("/selectCommentById/{id}")
    @ResponseBody
    public List<Bcomment> selectCommentById(@PathVariable("id")String id){
         return bcommentService.selectById(id);
    }

    @RequestMapping("/addcomment")
    @ResponseBody
    public ResponseMessage addcomment(Bcomment bcomment){
        if(bcomment.getcContent()==null||bcomment.getcContent()=="" || bcomment.getNickname()==null||bcomment.getNickname()==""){
            return ResponseMessage.success(404,"用户名或密码为空");
        }
        bcomment.setcTime(new Date());
        bcomment.setId(uuid.getUUId());
        bcommentService.addComment(bcomment);
        return ResponseMessage.success(202,"成功").addObject("comment",bcomment);
    }
}
